package com.huawei.aiosproj.view;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

public class DragAndDropCallback extends ItemTouchHelper.Callback {

    private final CardAdapter adapter;
    private final List<CardItem> cardItems; // Your list of data

    public DragAndDropCallback(CardAdapter adapter, List<CardItem> cardItems) {
        this.adapter = adapter;
        this.cardItems = cardItems;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        // Allow dragging only for the last item
        if (viewHolder.getAdapterPosition() == cardItems.size() - 1) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; // Or ItemTouchHelper.START | ItemTouchHelper.END for horizontal
            return makeMovementFlags(dragFlags, 0); // No swipe
        }
        return makeMovementFlags(0, 0); // No drag or swipe for other items
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();

        // Ensure we are moving the last item to the first position
        if (fromPosition == cardItems.size() - 1 && toPosition == 0) {
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(cardItems, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(cardItems, i, i - 1);
                }
            }
            adapter.notifyItemMoved(fromPosition, toPosition);
            // After moving, update the dataset and notify the adapter about the shift of other items
            // This is a simplified way. For complex scenarios, consider more specific notify calls.
            adapter.notifyItemRangeChanged(Math.min(fromPosition, toPosition), Math.abs(fromPosition - toPosition) + 1);
            return true;
        }
        return false; // Prevent other moves
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        // Swipe not used in this example
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true; // Enable long press to drag
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return false; // Disable swipe
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        // You can add visual cues when an item is selected for dragging
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            // viewHolder.itemView.setAlpha(0.5f); // Example: make item semi-transparent
        }
    }

    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        // Reset visual cues when dragging is finished
        // viewHolder.itemView.setAlpha(1.0f); // Example: reset transparency
    }
}